package com.lovo.test;

import java.util.Random;
import java.util.Scanner;

public class BullGame {
    private static Scanner scan = new Scanner(System.in);
    public static int[] computerArray = new int[4];
    private static int[] luckyArray = new int[4];
    private static int cishu = 5;

    public static void main(String[] args) {
        random();
        while (true) {
            shuru();
            int a = compare();
            if (a == 4) {
                System.out.println("你真棒，答对了");
                break;
            }
            if (cishu == 0) {
                System.out.println("次数用完啦，小垃圾");
                break;
            }
            System.out.println("你还有" + cishu + "机会");
        }

    }

    public static void random() {
        for (int i = 0; i < computerArray.length; i++) {
            int num = (int) (Math.random() * (9 - 0 + 1) + 0);//随机数
            boolean flag = true;//假设生成的随机数不相同
            for (int j = 0; j < i; j++) {
                if (computerArray[j] == num) {
                    flag = false;
                    i--;
                }
            }
            if (flag) {
                computerArray[i] = num;
            }
        }
        System.out.println("机选数：");
        for (int item : computerArray) {
            System.out.print(item + " ");
        }
        System.out.println("\n");
    }

    public static void shuru() {
        for (int i = 0; i < luckyArray.length; i++) {
            boolean flag = true;//输入的数与之前的数不重复
            while (true) {
                try {
                    System.out.println("请输入第" + (i + 1) + "个数");
                    int num = scan.nextInt();
                    for (int j = 0; j < i; j++) {
                        if (luckyArray[j] == num) {
                            System.out.println("输入的数重复，请重新输入");
                            i--;
                            flag = false;
                        }
                    }
                    if (flag) {
                        luckyArray[i] = num;
                    }
                    break;
                } catch (Exception e) {
                    scan.next();
                }
            }
        }
        cishu--;
        System.out.println("输入的数：");
        for (int item : luckyArray) {
            System.out.print(item + " ");
        }
        System.out.println("\n");
    }

    public static int compare() {
        int a = 0, b = 0;//计数工具
        for (int i = 0; i < computerArray.length; i++) {
            if (computerArray[i] == luckyArray[i]) {
                a++;
            } else {
                for (int j = 0; j < luckyArray.length; j++) {
                    if (computerArray[i] == luckyArray[j]) {
                        b++;
                    }
                }
            }
        }
        System.out.println(a + "A" + b + "B");
        return a;
    }
}
